package com.uplift.mgt.product.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.PageHelper;
import com.uplift.common.Const;
import com.uplift.common.utils.UUIDGenerater;
import com.uplift.mgt.product.dao.IProductMapper;
import com.uplift.mgt.product.dao.IProductPicMapper;
import com.uplift.mgt.product.model.Product;
import com.uplift.mgt.product.model.ProductPic;
import com.uplift.mgt.product.service.IProductService;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {

	@Autowired
	private IProductMapper productMapper;

	@Autowired
	private IProductPicMapper productPicMapper;
	
	@Override
	public List<Map<String, Object>> queryProduct(Product product) {
		PageHelper.startPage(product.getPage(), product.getLimit());
		return this.productMapper.selectProduct(product);
	}

	@Override
	public Map<String, Object> queryProductDetail(Product product) {
		List<Map<String, Object>> products = this.queryProduct(product);
		Map<String, Object> productMap = null;
		if (products != null && products.size() > 0) {
			productMap = products.get(0);
			// 查询详细图片
			ProductPic pp = new ProductPic();
			pp.setProductId(product.getId());
			List<Map<String, Object>> pics = this.productPicMapper.selectProductPic(pp);
			// 设置详细图片
			productMap.put("dpic", pics);
		}
		return productMap;
	}
	
	@Override
	public int saveProduct(Product product) {
		int ret = 0;
		try {
			// 保存商品主表信息
			// 处理封面图
			CommonsMultipartFile cmf = product.getPf();
			FileItem item = cmf.getFileItem();
			String fileName = item.getName();
			String fileExt = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
			String newFileName = UUIDGenerater.generate() + fileExt;
			String rfilePath = "product/";
			File newFileDir = new File(Const.DIR_UPLOAD_BASE, rfilePath);
			if (!newFileDir.exists()) {
				newFileDir.mkdirs();
			}
			File file = new File(newFileDir, newFileName);
			item.write(file);
			product.setPic(rfilePath + newFileName);
			product.setId(UUIDGenerater.generate());
			product.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			this.productMapper.insertProduct(product);
			
			// 保存商品详细图
			CommonsMultipartFile[] dcmfs = product.getDpf();
			List<ProductPic> productPics = new ArrayList<>();
			for(CommonsMultipartFile dcmf : dcmfs) {
				FileItem ditem = dcmf.getFileItem();
				String dfileName = ditem.getName();
				String dfileExt = dfileName.substring(dfileName.lastIndexOf(".")).toLowerCase();
				String dnewFileName = UUIDGenerater.generate() + dfileExt;
				String drfilePath = "product/";
				File dnewFileDir = new File(Const.DIR_UPLOAD_BASE, drfilePath);
				if (!dnewFileDir.exists()) {
					dnewFileDir.mkdirs();
				}
				File dfile = new File(dnewFileDir, dnewFileName);
				ditem.write(dfile);
				
				ProductPic productPic = new ProductPic();
				productPic.setId(UUIDGenerater.generate());
				productPic.setProductId(product.getId());
				productPic.setPic(drfilePath + dnewFileName);
				productPics.add(productPic);
			}
			this.productPicMapper.insertProductPic(productPics);
			ret = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	@Override
	public int removeProduct(Product product) {
		//this.productPicMapper.deleteByProductId(product.getId());
		return this.productMapper.deleteProduct(product);
	}

	@Override
	public int modifyProduct(Product product) {
		int ret = 0;
		try {		
			CommonsMultipartFile cmf = product.getPf();
			// 如果文件不等于空，则有新图片需要修改
			if (cmf != null) {
				FileItem item = cmf.getFileItem();
				String fileName = item.getName();
				String fileExt = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
				String newFileName = UUIDGenerater.generate() + fileExt;
				String rfilePath = "product/";
				File newFileDir = new File(Const.DIR_UPLOAD_BASE, rfilePath);
				if (!newFileDir.exists()) {
					newFileDir.mkdirs();
				}
				File file = new File(newFileDir, newFileName);
				item.write(file);
				// 设置图片路径
				product.setPic(rfilePath + newFileName);
			}
			
			// 保存商品详细图
			CommonsMultipartFile[] dcmfs = product.getDpf();
			if (dcmfs != null && dcmfs.length > 0) {
				List<ProductPic> productPics = new ArrayList<>();
				for(CommonsMultipartFile dcmf : dcmfs) {
					FileItem ditem = dcmf.getFileItem();
					String dfileName = ditem.getName();
					String dfileExt = dfileName.substring(dfileName.lastIndexOf(".")).toLowerCase();
					String dnewFileName = UUIDGenerater.generate() + dfileExt;
					String drfilePath = "product/";
					File dnewFileDir = new File(Const.DIR_UPLOAD_BASE, drfilePath);
					if (!dnewFileDir.exists()) {
						dnewFileDir.mkdirs();
					}
					File dfile = new File(dnewFileDir, dnewFileName);
					ditem.write(dfile);
					
					ProductPic productPic = new ProductPic();
					productPic.setId(UUIDGenerater.generate());
					productPic.setProductId(product.getId());
					productPic.setPic(drfilePath + dnewFileName);
					productPics.add(productPic);
				}
				this.productPicMapper.insertProductPic(productPics);
			}
			
			if (StringUtils.isNoneBlank(product.getDelDpfIds())) {
				String[] delDpfIdsArr = StringUtils.split(product.getDelDpfIds(), ",");
				List<String> delDpfIdsList = Arrays.asList(delDpfIdsArr);
				this.productPicMapper.deleteProductPic(delDpfIdsList);
			}
			
			// 修改商品主表信息
			this.productMapper.updateProduct(product);
			
			ret = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

}
